'
' Effority.Net ( http://www.effority.net )
' Benjamin Hermann, Oliver Dembniak
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Common.Utilities

Namespace effority.Ealo

    Public Class Utils
        Inherits EaloBase

#Region " Converter "

        Public Shared Function MakeStringList(ByVal reader As IDataReader) As Generic.List(Of StringInfo)

            Dim liste As New Generic.List(Of StringInfo)

            While reader.Read()

                Dim item As New StringInfo

                ' Id, Qualifier, StringName, StringText, Locale

                If Not reader.IsDBNull(0) Then
                    item.Id = reader.GetInt32(0)
                End If

                If Not reader.IsDBNull(1) Then
                    item.Qualifier = reader.GetString(1)
                End If

                If Not reader.IsDBNull(2) Then
                    item.StringName = reader.GetString(2)
                End If

                If Not reader.IsDBNull(3) Then
                    item.StringText = reader.GetString(3)
                    item.StringTextIsNull = False
                Else
                    item.StringTextIsNull = True
                End If

                If Not reader.IsDBNull(4) Then
                    item.Locale = reader.GetString(4)
                End If

                If Not reader.IsDBNull(5) Then
                    item.FallBack = reader.GetString(5)
                    item.FallbackIsNull = False
                Else
                    item.FallbackIsNull = True
                End If

                liste.Add(item)

            End While

            reader.Close()

            Return liste

        End Function

        Public Shared Function MakeStringDic(ByVal reader As IDataReader) As Generic.Dictionary(Of String, StringInfo)

            Dim liste As Generic.List(Of StringInfo) = effority.Ealo.Utils.MakeStringList(reader)

            Dim dic As Generic.Dictionary(Of String, StringInfo) = New Generic.Dictionary(Of String, StringInfo)

            If Not liste Is Nothing And liste.Count > 0 Then

                For Each item As StringInfo In liste

                    dic.Add(item.StringName, item)

                Next

            End If

            Return dic

        End Function

        Public Shared Function MakeStringInfo(ByVal reader As IDataReader) As StringInfo

            Dim item As StringInfo

            If reader.Read() Then

                item = New StringInfo
            
                ' Id, Qualifier, StringName, StringText, Locale

                If Not reader.IsDBNull(0) Then
                    item.Id = reader.GetInt32(0)
                End If

                If Not reader.IsDBNull(1) Then
                    item.Qualifier = reader.GetString(1)
                End If

                If Not reader.IsDBNull(2) Then
                    item.StringName = reader.GetString(2)
                End If

                If Not reader.IsDBNull(3) Then
                    item.StringText = reader.GetString(3)
                    item.StringTextIsNull = False
                Else
                    item.StringTextIsNull = True
                End If

                If Not reader.IsDBNull(4) Then
                    item.Locale = reader.GetString(4)
                End If

                If Not reader.IsDBNull(5) Then
                    item.FallBack = reader.GetString(5)
                    item.FallbackIsNull = False
                Else
                    item.FallbackIsNull = True
                End If

            End If

            reader.Close()

            Return item

        End Function

#End Region

#Region " Cache "

        Public Shared Sub StringInfoToCache(ByVal item As StringInfo)

            Dim CacheKeyQualifierAndStringNameAndLocale As String = EALO & SEPERATOR & item.Qualifier & SEPERATOR & item.StringName & SEPERATOR & item.Locale

            Dim CacheKeyQualifierAndLocale As String = EALO & SEPERATOR & item.Qualifier & SEPERATOR & item.Locale

            Dim CacheKeyId As String = EALO & SEPERATOR & "Id" & SEPERATOR & item.Id


            Dim dic As Generic.Dictionary(Of String, Ealo.StringInfo)

            If Not DataCache.GetCache(CacheKeyQualifierAndLocale) Is Nothing Then

                dic = CType(DataCache.GetCache(CacheKeyQualifierAndLocale), Generic.Dictionary(Of String, Ealo.StringInfo))

                If dic.TryGetValue(item.StringName, Nothing) Then

                    dic(item.StringName) = item

                Else

                    dic.Add(item.StringName, item)

                End If

            Else

                dic = New Generic.Dictionary(Of String, Ealo.StringInfo)

                dic.Add(item.StringName, item)

            End If

            DataCache.SetCache(CacheKeyQualifierAndStringNameAndLocale, item)

            DataCache.SetCache(CacheKeyQualifierAndLocale, dic)

            DataCache.SetCache(CacheKeyId, item)

            ' Cache Liste aktualisieren 

            Dim cacheList As Generic.List(Of String)

            If Not DataCache.GetCache(CacheKeyList) Is Nothing Then
                cacheList = CType(DataCache.GetCache(CacheKeyList), Generic.List(Of String))
            Else
                cacheList = New Generic.List(Of String)
            End If

            If Not cacheList.Contains(CacheKeyQualifierAndLocale) Then
                cacheList.Add(CacheKeyQualifierAndLocale)
            End If
            If Not cacheList.Contains(CacheKeyQualifierAndStringNameAndLocale) Then
                cacheList.Add(CacheKeyQualifierAndStringNameAndLocale)
            End If
            If Not cacheList.Contains(CacheKeyId) Then
                cacheList.Add(CacheKeyId)
            End If

            DataCache.SetCache(CacheKeyList, cacheList)

        End Sub

        Public Shared Sub ClearCache()

            Dim cacheList As Generic.List(Of String)

            If Not DataCache.GetCache(CacheKeyList) Is Nothing Then

                cacheList = CType(DataCache.GetCache(CacheKeyList), Generic.List(Of String))

                For Each key As String In cacheList

                    DataCache.RemoveCache(key)

                Next

            End If

        End Sub

#End Region

    End Class

End Namespace